{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "subject = 'sample' \n",
    "atlas = 'dkt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "argv: ['/home/npeled/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py', '-f', '/run/user/3628004/jupyter/kernel-70bd8741-d47c-4b5e-90db-49e141642a33.json']\n",
      "usage: ipykernel_launcher.py [-h] [-s SUBJECT] [--subjects SUBJECTS]\n",
      "                             [-a ATLAS] [--real_atlas REAL_ATLAS] [-b BIPOLAR]\n",
      "                             [-d DEBUG]\n",
      "\n",
      "MMVT\n",
      "\n",
      "optional arguments:\n",
      "  -h, --help            show this help message and exit\n",
      "  -s SUBJECT, --subject SUBJECT\n",
      "                        subject name\n",
      "  --subjects SUBJECTS   subjects names\n",
      "  -a ATLAS, --atlas ATLAS\n",
      "                        atlas name\n",
      "  --real_atlas REAL_ATLAS\n",
      "                        atlas name\n",
      "  -b BIPOLAR, --bipolar BIPOLAR\n",
      "                        bipolar\n",
      "  -d DEBUG, --debug DEBUG\n",
      "                        debug\n",
      "argv: ['/home/npeled/code/mmvt/src/mmvt_addon/scripts/run_mmvt.py', '-s', 'sample', '-a', 'dkt']\n",
      "*********** sample ***********\n",
      "Blender dir: /home/npeled/blender-2.79-linux-glibc219-x86_641\n",
      "start!\n",
      "After Blender call\n",
      "Finish! For more details look in /home/npeled/code/mmvt/logs/run_mmvt.log\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "error: unrecognized arguments: -f /run/user/3628004/jupyter/kernel-70bd8741-d47c-4b5e-90db-49e141642a33.json\n"
     ]
    }
   ],
   "source": [
    "import os, sys\n",
    "os.chdir('..')\n",
    "from src.mmvt_addon.scripts import run_mmvt\n",
    "run_mmvt.run(subject, atlas, run_in_background=False, debug=False, raise_exp=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No traceback available to show.\n"
     ]
    }
   ],
   "source": [
    "%tb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pizco import Proxy\n",
    "mmvt = Proxy('tcp://127.0.0.1:8001');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path as op\n",
    "import mne\n",
    "\n",
    "mne_sample_data_fol = mne.datasets.sample.data_path()\n",
    "meg_subject = 'sample'\n",
    "stc_fname = op.join(mne_sample_data_fol, 'MEG', 'sample', 'sample_audvis-meg-rh.stc')\n",
    "stc = mne.read_source_estimate(stc_fname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "from ipywidgets import HBox, VBox, Label, Button, Layout, Image\n",
    "\n",
    "T = stc.lh_data.shape[1] \n",
    "play = widgets.Play(value=0, min=0, max=T, step=1, interval=1000)\n",
    "time_slider = widgets.IntSlider(value=0, min=0, max=T, step=1)\n",
    "widgets.jslink((play, 'value'), (time_slider, 'value'))\n",
    "quality_slider = widgets.IntSlider(value=40, min=0, max=100, step=1, layout=Layout(width='100%'))\n",
    "zoom_slider = widgets.IntSlider(value=0, min=-5, max=5, step=1, layout=Layout(width='100%'))\n",
    "threshold_slider = widgets.FloatSlider(value=2, min=0, max=10, step=0.1, layout=Layout(width='20%'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def create_stc_t(stc, t):\n",
    "    data = np.concatenate([stc.lh_data[:, t:t + 1], stc.rh_data[:, t:t + 1]])\n",
    "    vertices = [stc.lh_vertno, stc.rh_vertno]\n",
    "    stc_t = mne.SourceEstimate(data, vertices, stc.tmin + t * stc.tstep, stc.tstep, subject=subject)\n",
    "    return stc_t\n",
    "\n",
    "def plot_stc(stc, t):\n",
    "#     return mmvt.plot_stc(stc, t, threshold=threshold_slider.value,  save_image=True)\n",
    "    stc_t = create_stc_t(stc, t)\n",
    "    stc_t_smooth = mne.morph_data(meg_subject, subject, stc_t, n_jobs=n_jobs, grade=vertices_to)\n",
    "    meg_fname = mmvt.plot_stc_t(stc_t_smooth.rh_data, stc_t_smooth.lh_data, t, threshold=threshold_slider.value,\n",
    "                                save_image=True, view_selected=False)\n",
    "    mmvt.set_current_t(t)\n",
    "    return meg_fname, stc_t_smooth\n",
    "    \n",
    "n_jobs = 4\n",
    "t = 0 # ms\n",
    "vertices_to = mne.grade_to_vertices(subject, None)    \n",
    "meg_fname, stc_t_smooth = plot_stc(stc, t);\n",
    "file = open(meg_fname, \"rb\")\n",
    "meg_image = Image(value=file.read(), format='png', width=800, height=450)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def update_image(fname=''):\n",
    "    fname = mmvt.save_image('stc', index=time_slider.value) if fname == '' else fname\n",
    "    file = open(fname, \"rb\")\n",
    "    meg_image.value = file.read()\n",
    "\n",
    "\n",
    "sagital_button = Button(description=\"Sagital\")\n",
    "sagital_button.on_click(lambda x:(mmvt.show_sagital(), mmvt.zoom(-1), update_image()))\n",
    "coronal_button = Button(description=\"Coronal\")\n",
    "coronal_button.on_click(lambda x:(mmvt.show_coronal(), mmvt.zoom(-1), update_image()))\n",
    "axial_button = Button(description=\"Axial\")\n",
    "axial_button.on_click(lambda x:(mmvt.show_axial(), mmvt.zoom(-1), update_image()))\n",
    "zoom_button = Button(description=\"zoom\")\n",
    "zoom_button.on_click(lambda x:(mmvt.zoom(zoom_slider.value), update_image()))\n",
    "render_button = Button(description='Render')\n",
    "update_button = Button(description='Update')\n",
    "update_button.on_click(lambda x:(update_image()))\n",
    "render_button.on_click(lambda x:(mmvt.hide_subcorticals(), mmvt.set_brain_transparency(1), mmvt.set_to_camera_view(),\n",
    "                                 update_image(mmvt.render_image(quality=quality_slider.value))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def on_value_change(change):\n",
    "    t = change['new']\n",
    "    meg_fname, stc_t_smooth = plot_stc(stc, t)\n",
    "    file = open(meg_fname, \"rb\")\n",
    "    meg_image.value = file.read()\n",
    "\n",
    "time_slider.observe(on_value_change, names='value') \n",
    "VBox([HBox([meg_image, VBox([sagital_button, coronal_button, axial_button, update_button,\n",
    "                             #Label('Zoom:'), zoom_slider, zoom_button,\n",
    "                             Label('Quality:'), quality_slider, render_button])]),\n",
    "     HBox([play, time_slider, Label('Threshold:'), threshold_slider])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "widgets": {
   "state": {
    "6380c829e07a4e9da693513cd2f0011b": {
     "views": [
      {
       "cell_index": 7
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
